package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.mapper.SysRoleMapper;
import com.itheima.ydd.service.SysMenuService;
import com.itheima.ydd.service.SysRoleMenuService;
import com.itheima.ydd.service.SysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Override
    public SysRoleDto getInfo(String id) {
        SysRole sysRole = getById(id);
        SysRoleDto roleDto = new SysRoleDto();
        //拷贝数据
        BeanUtils.copyProperties(sysRole, roleDto);
        LambdaQueryWrapper<SysRoleMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRoleMenu::getRoleId, id);
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.list(wrapper);
        //存放当前角色所管理的菜单(menu)
        List<SysMenu> menuList = new ArrayList<>();


        //遍历拿到一级菜单(pid=0)
        List<SysMenuDto> yimenuList = new ArrayList<>();
        if (roleMenuList != null) {
            for (SysRoleMenu sysRoleMenu : roleMenuList) {
                String menuId = sysRoleMenu.getMenuId();
                if (menuId != null) {
                    SysMenu sysMenu = sysMenuService.getById(menuId);
                    menuList.add(sysMenu);
                    //判断是不是一级菜单
                    if (sysMenu.getPid() == 0) {
                        SysMenuDto menuDto = new SysMenuDto();
                        //复制数据
                        BeanUtils.copyProperties(sysMenu, menuDto);
                        yimenuList.add(menuDto);
                    }
                }
            }
        }

        //遍历拿到当前用户的二级目录
        List<SysMenuDto> ermenuList = new ArrayList<>();
        for (SysMenuDto menuDto : yimenuList) {
            LambdaQueryWrapper<SysMenu> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SysMenu::getPid, menuDto.getId());
            List<SysMenu> sysMenuList = sysMenuService.list(wrapper1);
            for (SysMenu sysMenu : sysMenuList) {
                //如果menuList中包含此菜单就加强再收集起来
                if (menuList.contains(sysMenu)) {
                    SysMenuDto menuDto1 = new SysMenuDto();
                    BeanUtils.copyProperties(sysMenu, menuDto1);
                    ermenuList.add(menuDto1);
                }
            }
            //设置一级菜单下的二级菜单
            menuDto.setChildren(ermenuList);
        }
        //遍历拿到三级菜单
        List<SysMenu> sanmenuList = new ArrayList<>();
        for (SysMenuDto menu : ermenuList) {
            LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysMenuLambdaQueryWrapper.eq(SysMenu::getPid, menu.getId());
            List<SysMenu> list = sysMenuService.list(sysMenuLambdaQueryWrapper);
            for (SysMenu menu3 : list) {
                //如果menuIdList中包含此菜单就收集起来(无需加强了)
                if (menuList.contains(menu3)) {
                    sanmenuList.add(menu3);
                }
            }
            //给二级菜单的children赋值
            menu.setChildren(sanmenuList);
        }
        //最后一级菜单给roleDto的authList赋值
        roleDto.setAuthList(yimenuList);
        //返回结果
        return roleDto;
    }

    @Override
    public void del(String id) {
        //移除基本的角色信息
        this.removeById(id);
        //移除对应的菜单信息
        LambdaQueryWrapper<SysRoleMenu> menuWrapper = new LambdaQueryWrapper<>();
        menuWrapper.eq(SysRoleMenu::getRoleId , id);
        sysRoleMenuService.remove(menuWrapper);
    }

    @Override
    public void updateRole(SysRoleDto sysRoleDto) {
        //修改基本信息
        this.updateById(sysRoleDto);

        //修改菜单信息
        //删除原有的菜单信息
        LambdaQueryWrapper<SysRoleMenu> roleMenuWrapper = new LambdaQueryWrapper<>();
        roleMenuWrapper.in(SysRoleMenu::getRoleId , sysRoleDto.getId());
        sysRoleMenuService.remove(roleMenuWrapper);
        //添加信息
        List<String> role_menus = sysRoleDto.getRole_menus();
        List<SysRoleMenu> list = new ArrayList<>();
        for (String role_menu : role_menus) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(sysRoleDto.getId());
            sysRoleMenu.setMenuId(role_menu);
            list.add(sysRoleMenu);
        }

        sysRoleMenuService.saveBatch(list);
    }
}
